import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
class ProviderWidget<T extends ChangeNotifier> extends StatefulWidget {
  //控件对应的数据
  final T model;
  final Widget child;
  //邦定数据的控件
  final Widget Function(BuildContext context,T value,Widget child) builder;
  //数据初始化的方法
  final Function(T) onModelInit;
  const ProviderWidget(
      {@required this.model, this.child,@required this.builder,this.onModelInit}
      );
  @override
  _ProviderWidget createState() => _ProviderWidget<T>();
}

class _ProviderWidget<T extends ChangeNotifier> extends State<ProviderWidget<T>> {
  T model;
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    model=widget.model;
    print("初始化");
    if(widget.onModelInit!=null&&widget.model!=null){
    widget.onModelInit(model);
    }

  }
  @override
  Widget build(BuildContext context) {
    return ChangeNotifierProvider(
      create: (_)=>model,
      child: Consumer(builder: widget.builder,
      child: widget.child,
      ),
    );
  }
}



